/**
 * Copyright (c) 2017-2018, zengjintao (1913188966@qq.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fast.framework.ioc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.fast.framework.aop.GuiceManager;
import com.fast.framework.web.config.JFastConfig;

/**
 * ioc默认工厂
 * @author zengjintao
 * @version 1.0
 * 2017年10月13日下午8:29:38
 */
public class DefaultIocFactory implements IocFactory{

	//private static final Logger log = Logger.getLogger(DefaultIocFactory.class);
	
	private List<Class<?>> beanClass = new ArrayList<>();
	
	private static final GuiceManager guiceManger = GuiceManager.getGuiceManger();
	
	private static final Map<String, Object> beansMap = new ConcurrentHashMap<>();
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T getBean(String name) {
		return (T) beansMap.get(name);
	}

	public static Map<String, Object> getBeansmap() {
		return beansMap;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getBean(Class<?> clazz) {
		return (T) guiceManger.getInjector().getInstance(clazz);
	}

	@Override
	public void init(JFastConfig jFastConfig) {
//		List<Class<?>> clazzs = PackageUtil.scanPackage(jFastConfig.getPackageList());
//		for(Class cla : clazzs){
//			if(cla.getAnnotation(Bean.class) != null || cla.isAnnotationPresent(Controller.class)){
//				beanClass.add(cla);
//			}
//			continue;
//		}
//		initBean();
	}

	@SuppressWarnings("unused")
	private void initBean() {
//		for(Class cla : beanClass){
//			Object object = guiceManger.getInjector().getInstance(cla);
//			if(cla.isAnnotationPresent(Bean.class)){
//				Bean service = (Bean) cla.getAnnotation(Bean.class);
//				if(StrKit.isNotEmpoty(service.value())){
//					beansMap.put(service.value(), object);
//				}else{
//					beansMap.put(StrKit.toLowerCaseFirst(cla.getSimpleName()), object);
//				}
//			}
//			
//			log.info("created singleton bean '"+StrKit.toLowerCaseFirst(cla.getSimpleName())+"'");
//		}
//		initInject();
	}

	@SuppressWarnings("unused")
	private void initInject() {
//		for (Entry<String, Object> entry : beansMap.entrySet()) {
//			Object object = entry.getValue();
//			Field[] fields = object.getClass().getDeclaredFields();
//			for(Field field : fields){
//				try {
//					Inject inject = field.getAnnotation(Inject.class);
//					String name = inject.value();
//					field.setAccessible(true);
//					field.set(object, StrKit.isEmpoty(name) ? beansMap.get(field.getName()) : beansMap.get(name));
//					log.debug("created singleton bean '"+field.getName()+"'");
//				} catch (IllegalArgumentException | IllegalAccessException e) {
//					log.error("注入失败",e);
//				}
//			}
//		}
	}

	public List<Class<?>> getBeanClass() {
		return beanClass;
	}
}
